package com.example.togethermove.ui.activity.login;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


import com.example.togethermove.R;
import com.example.togethermove.base.Fields;
import com.example.togethermove.bean.login.LoginBean;
import com.example.togethermove.utils.StringUtilone;

import com.example.togethermove.utils.XmlUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.example.togethermove.utils.ToastUtil.showToast;

//登陆
public class LoginActivity extends AppCompatActivity {


    @BindView(R.id.et_phone)//输入手机号
            EditText et_phone;
    @BindView(R.id.et_checkCode)//输入验证码
            EditText et_checkCode;
    @BindView(R.id.tv_getCheckCode)//获取验证码
            TextView tv_getCheckCode;
    @BindView(R.id.tv_register)//注册
            TextView tv_register;
    @BindView(R.id.tv_login)//登陆
            TextView tv_login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);


    }

    @OnClick({R.id.et_phone, R.id.tv_register, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_headLeft:
                finish();
                break;
            case R.id.tv_register:
                finish();
                break;
            case R.id.tv_login:
                if (StringUtilone.isNull(et_phone.getText().toString()) || !StringUtilone.isMobileNO(et_phone.getText().toString())) {
                    showToast("请输入正确的手机号码!");
                } else if (et_checkCode.getText().toString().length() == 0) {
                    showToast(et_checkCode.getHint().toString());
                } else {
                    login();
                }
                break;

        }
    }

    public void login() {
        //都不为空,那么就可去请求数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
                    RequestBody body = new FormBody.Builder()
                            .add("keep_login", "1")
                            .add("phone", et_phone.getText().toString().trim())
                            .add("code", tv_getCheckCode.getText().toString().trim())
                            .build();
                    Request request = new Request.Builder()
                            .post(body)
                            .url("http://www.oschina.net/action/api/login_validate")
                            .build();
                    Response response = okHttpClient.newCall(request).execute();
                    // System.out.println(response.body().string());

                    //流只能读一次
                    LoginBean loginBean = XmlUtils.toBean(LoginBean.class, response.body().bytes());
                    //获取cookie
                    String token = response.header("set-cookie");
//                                loginBean.user.uid;

//                                loginBean.user.name

                    //参用sp保存
                    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Fields.sharePreferences.filename, Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString(Fields.sharePreferences.Token, token);
//                   edit.putString(Fields.sharePreferences.UserId,loginBean.user.uid);
//                  edit.putString(Fields.sharePreferences.name,loginBean.user.name);
                    edit.commit();

                    //关闭界面
                        finish();


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }







}